//
//  XKHomeSupportController.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/12.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKSupportController.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
@interface XKSupportController () <UIWebViewDelegate, NJKWebViewProgressDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NJKWebViewProgress *progressProxy;
@property (nonatomic, strong) NJKWebViewProgressView *progressView;
@property (nonatomic, assign, getter = isHaveTitle) BOOL haveTitle;
@end

@implementation XKSupportController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    _haveTitle = NO;
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, Main_Width, Main_Height-64)];
    _webView.scalesPageToFit = YES;
    _webView.scrollView.bounces = NO;
    NSURL *url = [NSURL URLWithString:_urlString];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.f;
    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.alpha = 0;
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController.navigationBar addSubview:_progressView];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_progressView removeFromSuperview];
}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress {
    _progressView.alpha = 1;
    [_progressView setProgress:progress animated:YES];
    
    
//    self.navigationItem.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
}



- (void)setUrlString:(NSString *)urlString {
    _urlString = urlString;
}

- (void)setTitleString:(NSString *)titleString {
    _titleString = titleString;
    
}


@end
